package cn.edu.sdjzu.xg.eagle.Controller;


import cn.edu.sdjzu.xg.eagle.dao.SchoolDaoImpl;
import cn.edu.sdjzu.xg.eagle.entity.Pagination;
import cn.edu.sdjzu.xg.eagle.entity.School;
import cn.edu.sdjzu.xg.eagle.exception.BaseException;
import cn.edu.sdjzu.xg.eagle.util.ControllerHelper;
import cn.edu.sdjzu.xg.eagle.util.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;

@WebServlet("/schoolRead")
public class SchoolReadCtl extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //设置响应格式
        response.setContentType("text/html;charset=utf-8");

        //创建JSON对象message，以便往前端响应信息
        JSONObject message = new JSONObject();

        //获取 id
        String id = request.getParameter("id");

        //将数值id转换为 Long型对象
        Long Long_id = Long.parseLong(id);

        try {
            //获取 School 实例
            School school = SchoolDaoImpl.getInstance().findSchool(Long_id);
            //将 School 对象转化为 JSON 对象
            String school_json = JSONObject.toJSONString(school, JSONWriter.Feature.ReferenceDetection);
            //响应 message 到前端
            response.getWriter().println(school_json);
        } catch (BaseException e) {
            message.put("message", e.getMessage());
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (SQLException e) {
            message.put("message", "数据库操作异常");
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (Exception e) {
            message.put("message", "网络异常");
            response.getWriter().println(message);
            e.printStackTrace();
        }
    }

    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");

        JSONObject message = new JSONObject();

        try {

            Collection<School> schoolList = SchoolDaoImpl.getInstance().findSchools();
            String schools_json = JSONObject.toJSONString(schoolList,JSONWriter.Feature.ReferenceDetection);
            response.getWriter().println(schools_json);

        } catch (BaseException e) {
            message.put("message", e.getMessage());
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (SQLException e) {
            message.put("message", "数据库操作异常");
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (Exception e) {
            message.put("message", e.getMessage());
            response.getWriter().println(message);
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("application/json;charset=utf-8");
        JSONObject message = new JSONObject();
        String req_json = JSONUtil.getJSON(request);
        System.out.println("初始请求体内容：" + req_json);
        JSONObject req_jsonObject = JSONObject.parseObject(req_json);

        if (req_jsonObject == null) {
            message.put("message","未收到请求");
            response.getWriter().println(message);
            return;
        }
        try {
            Pagination pagination = ControllerHelper.getPagination(req_jsonObject);
            Collection<School> schools = SchoolDaoImpl.getInstance().findSchoolsWithPagination(pagination);
            String schools_json = JSONObject.toJSONString(schools,JSONWriter.Feature.ReferenceDetection);
            message.put("totalNum", pagination.getTotalNum());
            message.put("data", schools_json);
            response.getWriter().println(message);
        } catch (SQLException e) {
            message.put("message","数据库操作异常");
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (BaseException e) {
            message.put("message", e.getMessage());
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (Exception e) {
            message.put("message","网络异常");
            response.getWriter().println(message);
            e.printStackTrace();
        }
    }
}
